function orderData() {
    $.ajax({
        url: "findOrderByOrderStateData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code == 500) {
                Swal.fire("温馨提示", "未接订单", "warning");
                $("#accordion").html("");
            } else {
                var code = '';
                for (var i = 0; i < data.order_list.length; i++) {
                    code += "  <div class=\"card\">\n" +
                        "                                <div class=\"card-header\">\n" +
                        "                                    <a class=\"heading-title collapsed\" data-toggle=\"collapse\" href=\"#collapseOne" + data.order_list[i].orderID + "\"\n" +
                        "                                       role=\"button\" aria-expanded=\"false\" onclick=\"checkData(" + data.order_list[0].orderID + ")\">\n" +
                        "                                        " + data.order_list[i].orderName + "\n" +
                        "                                    </a>\n" +
                        "                                </div>\n" +
                        "\n" +
                        "                                <div id=\"collapseOne" + data.order_list[i].orderID + "\" class=\"collapse\" data-parent=\"#accordion\">\n" +
                        "\n" +
                        "                                </div>\n" +
                        "                            </div>";
                }
                $("#accordion").html("");
                $("#accordion").append(code);
            }
        }
    });

}

function checkData(orderID) {
    var DataSources = {
        "orderID": orderID
    };
    $.ajax({
        url: "findOrderProductByOrderProductData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 500) {

            } else {
                var code = '';
                for (var i = 0; i < data.orderProduct_list.length; i++) {
                    code += "                             <div class=\"list-view-layout1\">\n" +
                        "                                                            <div class=\"product-box-layout3\">\n" +
                        "                                                                <div class=\"item-img\">\n" +
                        "                                                                    <a href=web-single-product.html?id=" + data.orderProduct_list[i].recProductID + "><img\n" +
                        "                                                                          style='width: 275px;height: 195px;' src='upload/" + data.orderProduct_list[i].recycleProductEntity.recProductImage + "' alt=\"Product\"></a>\n" +
                        "                                                                </div>\n" +
                        "                                                                <div class=\"product-info\">\n" +
                        "                                                                    <div class=\"item-content\">\n" +
                        "                                                                        <h3 class=\"item-title\"><a href=web-single-product.html?id=" + data.orderProduct_list[i].recProductID + ">" + data.orderProduct_list[i].recycleProductEntity.recProductName + "\n" +
                        "                                                                            </a><span></span></h3>\n" +
                        "                                                                        <ul class=\"entry-meta\">\n" +
                        "                                                                            <li><i class=\"far fa-clock\"></i>" + data.orderProduct_list[i].orderProductDate + "</li>\n" +
                        "                                                                            </li>\n" +
                        "                                                                        </ul>\n" +
                        "                                                                        <ul class=\"item-condition\">" +
                        " <li><span>废品量:</span> </li>\n" +
                        "                                                        <li><input value='" + data.orderProduct_list[i].orderProductNum + "' id='order_" + data.orderProduct_list[i].orderProductID + "'/></li>\n" +
                        "                                                                        </ul>\n" +
                        "                                                                        <div class=\"btn-group\">\n" +
                        "                                                                        </div>\n" +
                        "                                                                    </div>\n" +
                        "                                                                    <div class=\"item-right\">\n" +
                        "                                                                        <div class=\"item-price\">\n" +
                        "                                                                            <span class=\"currency-symbol\">$</span>\n" +
                        "                                                                          " + data.orderProduct_list[i].recycleProductEntity.recProductPrice + "\n" +
                        "                                                                        </div>" +
                        "<div class=\"item-btn\">\n" +
                        "                                                        <a onclick='orderUpdate_1(" + data.orderProduct_list[i].orderProductID + ")'>确认</a>\n" +
                        "                                                    </div>\n" +
                        "                                                                    </div>\n" +
                        "                                                                </div>\n" +
                        "                                                            </div>\n" +
                        "                                                        </div>\n";
                }
                var detail = '';
                detail += "                                                        <div class=\"pagination-layout1\">\n" +
                    "                                                            <div class=\"btn-prev disabled\">\n" +
                    "\n" +
                    "                                                            </div>\n" +
                    "                                                            <div class=\"page-number\">\n" +
                    "\n" +
                    "                                                            </div>\n" +
                    "                                                            <div class=\"btn-next\">\n" +
                    "                                                                <a onclick='orderUpdate(" + orderID + ")'>发送账单<i class=\"fas fa-angle-double-right\"></i></a>\n" +
                    "                                                            </div>\n" +
                    "                                                        </div>";

                $("#collapseOne" + orderID).html("");
                $("#collapseOne" + orderID).append(code);
                $("#collapseOne" + orderID).append(detail);
            }
        }
    });
}

var numPrice = 0;

function orderUpdate(orderID) {
    var DataSources = {
        "orderID": orderID
    };
    $.ajax({
        url: "findOrderProductByOrderProductData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            for (var i = 0; i < data.orderProduct_list.length; i++) {
                numPrice += data.orderProduct_list[i].orderProductNum * data.orderProduct_list[i].recycleProductEntity.recProductPrice;
            }
            updateOrderByOrderPrice(orderID);
        }
    });
}

function updateOrderByOrderPrice(orderID) {
    var DataSources = {
        "orderID": orderID,
        "orderPrice": numPrice
    };
    $.ajax({
        url: "updateOrderByOrderPrice",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 200) {
                orderState(orderID);
            } else {
                Swal.fire("温馨提示", "废品订单生成失败", "error");
            }
        }
    });
}

function orderState(orderID) {
    var DataSources = {
        "orderID": orderID,
        "orderState": 3
    };
    $.ajax({
        url: "updateOrderByOrderState",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 200) {
                Swal.fire("温馨提示", "废品订单生成成功", "success");
                orderData();
            } else {
                Swal.fire("温馨提示", "废品订单生成失败", "error");
            }
        }
    });
}

function orderUpdate_1(orderProductID) {
    var orderProductNum = $("#order_" + orderProductID).val();
    var DataSources = {
        "orderProductID": orderProductID,
        "orderProductNum": orderProductNum
    };
    $.ajax({
        url: "updateOrderProductByOrderProductNum",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 200) {
                Swal.fire("温馨提示", "废品量录入成功", "success");
            } else {
                Swal.fire("温馨提示", "废品量录入失败", "error");
            }
        }
    });
}

$(function () {
    var DATA = '';
    $.ajax({
        url: "JudgeLogin_3",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code == 500) {
                DATA += " <li class=\"header-btn\">\n" +
                    "                                    <a href=\"web-login.html\" class=\"item-btn\"><i class=\"fas fa-plus\"></i>登录</a>\n" +
                    "                                </li>"
                Swal.fire("温馨提示", "请先登录", "warning");
                window.location.href = "web-user-404.html";
            } else {
                DATA += " <li class=\"header-login-icon\">\n" +
                    "                                    <a href=\"web-user-account.html\" class=\"color-light\" data-toggle=\"tooltip\"\n" +
                    "                                       data-placement=\"top\" title=\"Login/Register\">\n" +
                    "                                        <i class=\"far fa-user\">我的</i>\n" +
                    "                                    </a>\n" +
                    "                                </li>\n" +
                    "                                <li class=\"header-btn\">\n" +
                    "                                    <a  onclick='tuichu_1()' class=\"item-btn\"><i class=\"fas fa-plus\"></i>退出</a>\n" +
                    "                                </li>";
                orderData();
            }
            $("#userData").html("");
            $("#userData").append(DATA);
        }
    });
});